package com.cloud.wms.ui.returned.presenter;

import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.http.error.ApiException;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.main.entity.CommodityEntity;
import com.cloud.wms.ui.returned.contact.RelayContract;
import com.cloud.wms.ui.returned.entity.RelayBoxEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

/**
 * Created by FC on 2021/5/13.
 * Desc：
 */
public class RelayPresenter extends BaseRxPresenter<RelayContract.View> implements RelayContract.presenter {
    public static final int GET_BOX_COMMODITY = 0x100;
    public static final int CONFIRM_RETURN_RELAY = 0x101;

    @Inject
    public RelayPresenter() {

    }

    @Override
    public void getBoxCommodityInfo(String boxCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("ContainerCode", boxCode);
        FineExApplication.component().http()
                .getContainCommodityMsg(params)
                .map(new ResponseWrapper<>())
                .flatMap((Function<List<RelayBoxEntity>, ObservableSource<List<RelayBoxEntity>>>) boxEntities -> {
                    if (boxEntities == null || boxCode.isEmpty()) {
                        return Observable.error(new ApiException("当前容器未查询到清点明细"));
                    }
                    for (int i = 0; i < boxEntities.size(); i++) {
                        String code = boxEntities.get(i).getContainerCode();
                        if (boxCode.equalsIgnoreCase(code)) {
                            boxEntities.get(i).setScan(true);
                            break;
                        }
                    }
                    return Observable.just(boxEntities);
                })
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<List<RelayBoxEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(List<RelayBoxEntity> boxEntities) {
                        mView.onSuccess(MessageCreator.createMessage(boxEntities, GET_BOX_COMMODITY));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    @Override
    public void confirmRelay(List<RelayBoxEntity> boxList) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("userId", FineExApplication.component().loginCache().getUserInfo().getUserID());
        ArrayList<HashMap<String, Object>> boxMaps = new ArrayList<>();
        for (int i = 0; i < boxList.size(); i++) {
            RelayBoxEntity boxEntity = boxList.get(i);
            List<CommodityEntity> commodityList = boxEntity.getCommodityList();
            HashMap<String, Object> boxMap = new HashMap<>();
            boxMap.put("containerCode", boxEntity.getContainerCode());
            ArrayList<HashMap<String, Object>> commodityMaps = new ArrayList<>();
            for (int j = 0; j < commodityList.size(); j++) {
                CommodityEntity commodityEntity = commodityList.get(j);
                params.put("memberID", commodityEntity.getMemberID());
                HashMap<String, Object> map = new HashMap<>();
                map.put("commodityID", commodityEntity.getCommodityId());
                map.put("stockType", commodityEntity.getStockType());
                map.put("productionDate", commodityEntity.getProductionDate());
                map.put("expirationDate", commodityEntity.getExpirationDate());
                map.put("inDate", commodityEntity.getInDate());
                map.put("amount", commodityEntity.getAmount());
                map.put("productBatchID", commodityEntity.getProductBatchID());
                map.put("logisticsCode", commodityEntity.getLogisticsCode());
                commodityMaps.add(map);
            }
            boxMap.put("commodityList", commodityMaps);
            boxMaps.add(boxMap);
        }
        params.put("DeliveryList", boxMaps);
        FineExApplication.component().http()
                .saveReturnRelay(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Object>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Object info) {
                        mView.onSuccess(MessageCreator.createMessage(info, CONFIRM_RETURN_RELAY));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
